using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;
using System.IO;

/// <summary>
/// Summary description for Class1
/// </summary>
public class HttpBaseClass
{
    private string UserName;
    private string UserPwd;
    private string ProxyServer;
    private int ProxyPort;
    private string Request;

    public HttpBaseClass(string HttpUserName, 
        string HttpUserPwd, string HttpProxyServer, 
        int HttpProxyPort, string HttpRequest)
	{
		//
		// TODO: Add constructor logic here
		//
        UserName = HttpUserName;
        UserPwd = HttpUserPwd;
        ProxyServer = HttpProxyServer;
        ProxyPort = HttpProxyPort;
        Request = HttpRequest;
	}

    public virtual HttpWebRequest CreateWebRequest(string uri, NameValueCollection collHeader, 
        string RequestMethod, bool NwCred)
    {
        HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
        webrequest.KeepAlive = false;
        webrequest.Timeout = 15 * 1000; //15s
        webrequest.ReadWriteTimeout = 15 * 1000;
        webrequest.Method = RequestMethod;

        int iCount = collHeader.Count;
        string key;
        string keyvalue;

        for (int i = 0; i < iCount; ++i)
        {
            key = collHeader.Keys[i];
            keyvalue = collHeader[i];
            webrequest.Headers.Add(key, keyvalue);
        }

        webrequest.ContentType = "text/html";

        if (ProxyServer.Length > 0)
        {
            webrequest.Proxy = new WebProxy(ProxyServer, ProxyPort);
        }
        webrequest.AllowAutoRedirect = false;

        if (NwCred)
        {
            CredentialCache wrCache = new CredentialCache();
            wrCache.Add(new Uri(uri), "Basic", new NetworkCredential(UserName, UserPwd));
            webrequest.Credentials = wrCache;
        }

        collHeader.Clear();
        return webrequest;
    }

    public virtual string GetRedirectURL(HttpWebResponse webresponse, ref string Cookie)
    {
        string uri = "";

        WebHeaderCollection headers = webresponse.Headers;

        if((webresponse.StatusCode == HttpStatusCode.Found) ||
            (webresponse.StatusCode == HttpStatusCode.Redirect) ||
            (webresponse.StatusCode == HttpStatusCode.Moved) ||
            (webresponse.StatusCode == HttpStatusCode.MovedPermanently))
        {
            uri = headers["Location"];
            uri = uri.Trim();
        }

        if (headers["Set-Cookie"] != null)
        {
            Cookie = headers["Set-Cookie"];
        }
        return uri;
    }

    public virtual string GetFinalResponse(string ReUri, 
        string Cookie, string RequestMethod, bool NwCred)
    {
        NameValueCollection collHeader = new NameValueCollection();

        if (Cookie.Length > 0)
        {
            collHeader.Add("Cookie", Cookie);
        }

        HttpWebRequest webrequest = CreateWebRequest(ReUri, collHeader, RequestMethod, NwCred);
        BuildReqStream(ref webrequest);

        HttpWebResponse webresponse;
        webresponse = (HttpWebResponse)webrequest.GetResponse();

        Encoding enc = System.Text.Encoding.GetEncoding(1252);
        StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc);

        string Response = loResponseStream.ReadToEnd();

        loResponseStream.Close();
        webresponse.Close();

        return Response;
    }

    private void BuildReqStream(ref HttpWebRequest webrequest)
    {
        byte[] bytes = Encoding.ASCII.GetBytes(Request);
        webrequest.ContentLength = bytes.Length;

        Stream oStreamOut = webrequest.GetRequestStream();
        oStreamOut.Write(bytes, 0, bytes.Length);
        oStreamOut.Close();
    }
}
